/*
 * @Author: Kang
 * @Date: 2022-04-19 23:37:17
 * @Last Modified by: Kang
 * @LastEditTime: 2022-04-20 00:05:16
 */
'use strict';

const Service = require('egg').Service;
const nodemailer = require('nodemailer');

class EmailUtilService extends Service {
  
  async sendMail(phone, subject,text) { //nodemailer方式

    const transport = nodemailer.createTransport({
      host: this.config.email.host,
      // secureConnection: true, // use SSL
      port: 465,
      secure: true,
      auth: {
        user: this.config.email.name,
        pass: this.config.email.password
      }
    });
    
    const message = {
      from: this.config.email.name, // Sender address
      to: this.config.email.to,         // List of recipients
      subject: subject, // Subject line
      html: `公司名称：${text.companyName}<br>客户姓名：${text.name}<br>联系电话：${text.phone}<br>邮箱：${text.email}<br>咨询内容：${text.content}<br>提交时间：${this.ctx.helper.formatTime()}` // Plain text body
    };
    
    console.log('message:', message);
    // console.log('transport:', transport);
    // transport.sendMail(message, function (err, info) {
    //   if (err) {
    //     console.log('err:');
    //     console.log(err);
    //   } else {
    //     console.log('info',info);
    //     return info;
    //   }
    // });
    try {
      let res = transport.sendMail(message)
      console.log(res)
      return res
    } catch (error) {
      throw new Error(error);
    }
  }
}

module.exports = EmailUtilService;
